package io.gitee.tuo_tuo.jfinaldevassistance.json;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.json.FastJson;
import com.jfinal.json.JFinalJson;
import com.jfinal.json.Json;
import io.gitee.tuo_tuo.jfinaldevassistance.common.Rst;

import java.util.Map;

/**
 * 类名称：
 * 描述:
 * 版权: Copyright (c) 2018
 * 公司:
 * 作者: tuotuo
 * 版本: 1.0
 * 创建日期: 2018/7/12
 * 创建时间: 10:12
 */
public class IntelligenceSelectJson extends Json {

	private static FastJson fastJson = FastJson.getJson();

	@Override
	public String toJson(Object object) {
		if(object instanceof Rst){
			return Null2EmptyStringJFinalJson.getJson().toJson(object);
		}
		return JFinalJson.getJson().toJson(object);
	}

	@Override
	public <T> T parse(String jsonString, Class<T> type) {
		if(type == Rst.class){
			JSONObject jsonObject = JSONObject.parseObject(jsonString);
			Map<String, Object> innerMap = jsonObject.getInnerMap();
			int code = (int)innerMap.remove(Rst.CODE);
			String msg = (String) innerMap.remove(Rst.MSG);
			Rst rst = new Rst(code).setMsg(msg);
			rst.putAll(innerMap);
			return (T) rst;
		}
		return fastJson.parse(jsonString, type);
	}
}
